package com.zksite.nator.client.config;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.zksite.nator.common.config.Application;
import com.zksite.nator.common.config.Config;
import com.zksite.nator.common.util.Reflections;

@Component
public class TextMappingConfig implements MappingConfig {

	private Config config = null;

	public Config getConfig() {
		synchronized (this) {
			if (config == null) {
				try {
					init();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return config;
	}

	@SuppressWarnings("resource")
	public void init() throws Exception {
		URI uri = TextMappingConfig.class.getClassLoader().getResource("config.cnf").toURI();
		BufferedReader fileReader = new BufferedReader(new FileReader(new File(uri)));
		String content = null;
		config = new Config();
		List<Application> list = new ArrayList<Application>(0);
		while ((content = fileReader.readLine()) != null) {

			if (StringUtils.isBlank(content) || content.indexOf("#") != -1) {
				continue;
			}
			String[] strings = StringUtils.split(content);
			if (strings.length == 1) {
				if (content.indexOf("server") != -1) {
					String[] split = content.split(":");
					if (split.length != 2) {
						throw new IllegalStateException("配置文件有误");
					}
					Reflections.setFieldValue(config, split[0].trim(), split[1].trim());
				}
			} else if (strings.length >= 3) {
				Application application = new Application();
				String domain = strings[0].trim();
				String ip = strings[1].trim();
				String port = strings[2].trim();
				String name = "";
				if (strings.length == 4) {
					name = strings[3].trim();
				}
				application.setDomain(domain);
				application.setHost(ip);
				application.setName(name);
				try {
					application.setPort(Integer.valueOf(port));
				} catch (Exception e) {
					throw new IllegalStateException("应用端口配置有误");
				}
				list.add(application);
			}
		}
		processApplications(list);
		config.setApplications(list);
		fileReader.close();
	}

	private void processApplications(List<Application> list) {
		List<Application> newList = new ArrayList<Application>();
		for (Application application : list) {
			String domain = application.getDomain();
			String[] strings = domain.split("\\.");
			if (strings.length == 2) {
				Application www = new Application();
				www.setDomain("www." + domain);
				www.setHost(application.getHost());
				www.setName(application.getName());
				www.setPort(application.getPort());
				newList.add(www);
			}
		}
		list.addAll(newList);
	}

	public Application getApplication(String host) {
		if (config == null) {
			throw new IllegalStateException();
		}
		List<Application> applications = config.getApplications();
		for (Application application : applications) {
			if (application.getDomain().equals(host)) {
				return application;
			}
		}
		return null;
	}

}
